﻿
import requests
import itchat
from itchat.content import *
import json
import time
import datetime
import threading

def loop():
    sched_time = str(datetime.datetime(2018, 3,7,13,0,0))
    morning_time=str(datetime.datetime(2018, 3,7,18,0,0))
    while True:
        time.sleep(1)
        now = str(datetime.datetime.now())[:-7]
        if now==sched_time:
            sched_time = str(datetime.datetime.now() + datetime.timedelta(hours=1))[:-7]
            if now==morning_time:
                hello={'00':'晚安','06':'早安','12':'中午好','18':'下午好'}
                hello_words=hello[str(morning_time)[11:13]]
                morning_time = str(datetime.datetime.now() + datetime.timedelta(hours=6))[:-7]
                users = itchat.search_friends(name='馨儿')
                userName = users[0]
                chatroom = itchat.search_chatrooms(name='宝宝躺下了')[0]
                itchat.send_msg(hello_words, userName['UserName'])
                itchat.send_msg(hello_words, chatroom['UserName'])
                if str(morning_time)[11:13]=='06':
                    replycontent = task('成都今天天气怎么样')
                    itchat.send_msg(replycontent, chatroom['UserName'])
                    itchat.send_msg(replycontent, userName['UserName'])
            else:
                users = itchat.search_friends(name='馨儿')
                userName = users[0]
                replycontent = task('现在几点')
                itchat.send_msg(replycontent, userName['UserName'])
                chatroom=itchat.search_chatrooms(name='宝宝躺下了')[0]
                itchat.send_msg('整点报时', chatroom['UserName'])
t1 = threading.Thread(target=loop, name='LoopThread')
t1.start()

KEY = 'b31264c901684cd39386fc2c9699c9ba'
def get_response(msg):
    apiUrl = 'http://www.tuling123.com/openapi/api'
    data = {
        'key'    : KEY,
        'info'   : msg,
        'userid' : 'wechat-robot',
    }
    try:
        r = requests.post(apiUrl, data=data).json()
        return r.get('text')
    except:
        return

def task(question):
    return get_response(question)

@itchat.msg_register([PICTURE,TEXT])
def simple_reply(msg):
    if msg['User']['NickName']=='未曾只因':
        reply = get_response(msg['Text'])
        return reply
    if str(msg['Text'])[0]=='！':
        reply = get_response(msg['Text'])
        return reply
itchat.auto_login(enableCmdQR=2)
itchat.run()

